<?php
namespace App\Intelihelp\Controller;

use Foxframe\Paginator;

use DoctrineExtensions\Paginate\Paginate;

use Foxframe\Filter;

use Foxforms\Widget\Field\Radio;

use Foxforms\Widget\Field\Select;

use Foxforms\Widget\Field\TextArea;

use Foxforms\Widget\Form;
use Foxforms\Widget\Field\Input;

class tarefaController extends \Foxframe\Controller
{
	public function index($params = array())
	{
		$this->redirect(BASE_PATH.'intelihelp/tarefa/viewListarTarefasPendentesMinhas');
	}

	public function viewListarTarefasPendentesMinhas($params = array())
	{
		$criteria	= array(
			's.fl_situacao'	=> 1,
			't.responsavel'	=> unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa()
		);
		$filter = new Filter($criteria);
		$query = $this->em->getRepository('Entities\Intelihelp\Tarefa')->listar($filter->getQueryFilter());
// 		if (isset($params['page']))
// 			$filter->setActualPage($params['page']);

// 		$paginator = new Paginator($query, $filter->getActualPage(), $filter->getMaxResults(), $this->uri->getUrl().'/page/');

// 		$this->view->assign('paginator', $paginator);

// 		if (isset($params['page']))
// 			return $this->returnAjax(null,null,'viewGridTarefa');
		$this->view->assign('query', $query->getResult());
	}

	public function viewListarTarefasPendentes($params = array())
	{
		$criteria	= array(
			's.fl_situacao'	=> 1,
			'p.id_pessoa'	=> unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa()
		);
		$filter = new Filter($criteria);
		$query = $this->em->getRepository('Entities\Intelihelp\Tarefa')->listar($filter->getQueryFilter());
// 		if (isset($params['page']))
// 			$filter->setActualPage($params['page']);

// 		$paginator = new Paginator($query, $filter->getActualPage(), $filter->getMaxResults(), $this->uri->getUrl().'/page/');

// 		$this->view->assign('paginator', $paginator);

// 		if (isset($params['page']))
// 			return $this->returnAjax(null,null,'viewGridTarefa');
		$this->view->assign('query', $query->getResult());
	}

	public function viewListarTarefasSolicitadas($params = array())
	{
		$criteria	= array(
			's.fl_situacao'	=> 1,
			'sol.id_pessoa'	=> unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa()
		);
		$filter = new Filter($criteria);
		$query = $this->em->getRepository('Entities\Intelihelp\Tarefa')->listar($filter->getQueryFilter());
// 		if (isset($params['page']))
// 			$filter->setActualPage($params['page']);

// 		$paginator = new Paginator($query, $filter->getActualPage(), $filter->getMaxResults(), $this->uri->getUrl().'/page/');

// 		$this->view->assign('paginator', $paginator);

// 		if (isset($params['page']))
// 			return $this->returnAjax(null,null,'viewGridTarefa');
		$this->view->assign('query', $query->getResult());
	}

	public function viewListarTarefasFechadas($params = array())
	{
		$criteria	= array(
			's.fl_situacao'	=> 0,
			'p.id_pessoa'	=> unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa()
		);
		$filter = new Filter($criteria);
		$query = $this->em->getRepository('Entities\Intelihelp\Tarefa')->listar($filter->getQueryFilter());

// 		if (isset($params['page']))
// 			$filter->setActualPage($params['page']);

// 		$paginator = new Paginator($query, $filter->getActualPage(), $filter->getMaxResults(), $this->uri->getUrl().'/page/');
// 		$this->view->assign('paginator', $paginator);

// 		if (isset($params['page']))
// 			return $this->returnAjax(null,null,'viewGridTarefa');
		$this->view->assign('query', $query->getResult());
	}

	public function viewListarTarefasFechadasSolicitadas($params = array())
	{
		$criteria	= array(
			's.fl_situacao'	=> 0,
			't.solicitante'	=> unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa()
		);
		$filter = new Filter($criteria);
		$query = $this->em->getRepository('Entities\Intelihelp\Tarefa')->listar($filter->getQueryFilter());
// 		if (isset($params['page']))
// 			$filter->setActualPage($params['page']);

// 		$paginator = new Paginator($query, $filter->getActualPage(), $filter->getMaxResults(), $this->uri->getUrl().'/page/');
// 		$this->view->assign('paginator', $paginator);

// 		if (isset($params['page']))
// 			return $this->returnAjax(null,null,'viewGridTarefa');
		$this->view->assign('query', $query->getResult());
	}

	public function viewListarTarefasFechadasMinhas($params = array())
	{
		$criteria	= array(
			's.fl_situacao'	=> 0,
			't.responsavel'	=> unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa()
		);
		$filter	= new Filter($criteria);
		$query	= $this->em->getRepository('Entities\Intelihelp\Tarefa')->listar($filter->getQueryFilter());
// 		if (isset($params['page']))
// 			$filter->setActualPage($params['page']);

// 		$paginator = new Paginator($query, $filter->getActualPage(), $filter->getMaxResults(), $this->uri->getUrl().'/page/');
// 		$this->view->assign('paginator', $paginator);

// 		if (isset($params['page']))
// 			return $this->returnAjax(null,null,'viewGridTarefa');
		$this->view->assign('query', $query->getResult());
	}

	public function viewCadastrar($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tarefa	= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		else
			$tarefa	= new \Entities\Intelihelp\Tarefa();

		$form	= new Form();
		$form->addValues($tarefa, 'tarefa');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tarefa'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveTarefa'));
		$form->addElement(Input::create('tarefa[version]')->setType('hidden'));
		$form->addElement(
				Input::create('tarefa[solicitante]')
					->setType('hidden')
					->setValue(unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa())
		);
		$form->addElement(Input::create('tarefa[id_tarefa]')->setType('hidden'));
		$form->addElement(
			Input::create('tarefa[dt_prazo]')
				->setType('text')
				->setLabel('Prazo')
				->addClass('datepicker')
				->validate()
		);
		$form->addElement(
			Select::create('tarefa[tipo]')
				->setLabel('Tipo')
				->validate()
				->setOption($this->em->getRepository('\Entities\Intelihelp\Tipo')->listCombo())
				->setPlaceHolder('-- Selecione --')
		);
		$form->addElement(
			Select::create('tarefa[equipe]')
				->setLabel('Equipe')
				->validate()
				->setOption($this->em->getRepository('\Entities\Intelihelp\Equipe')->listComboExecutor())
				->setPlaceHolder('-- Selecione --')
		);
		$form->addElement(
			Input::create('tarefa[ds_titulo]')
				->setLabel('Titulo')
				->validate()
				->setMaxLength(255)
		);
		$form->addElement(TextArea::create('tarefa[ds_tarefa]')->setLabel('Tarefa')->validate()->addClass('simpleRich'));
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		$this->view->assign('tarefa', $tarefa);

		return $this->returnAjax();
	}

	public function saveTarefa($params = array())
	{
		try
		{
			if (!isset($params['tarefa']['id_tarefa']) || empty($params['tarefa']['id_tarefa']))
			{
				$params['tarefa']['solicitante']	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();
				$params['tarefa']['status']			= \Entities\Intelihelp\Status::ABERTO;
			}
			$service	= new \Foxframe\Service();
			$tarefa		= $service->save($params['tarefa'], '\Entities\Intelihelp\Tarefa');
			$this->em->flush();

			$email = new \App\Intelihelp\Controller\mailController();
			if (isset($params['tarefa']['id_tarefa']) && !empty($params['tarefa']['id_tarefa']))
			{
				$email->tarefaAtualizada(array(
						'tarefa'	=> $tarefa
					)
				);
			}
			else
			{
				$email->tarefaCadastrada(array(
						'tarefa'	=> $tarefa
					)
				);
			}

			$this->setFlash('Success', 'Sucesso', "Tarefa N°. {$tarefa->getIdTarefa()} salva com sucesso");
		}
		catch (Exception $e)
		{
			$this->setFlash('Error', 'Erro ao salvar tarefa', $e->getMessage());
		}

		if (isset($params['redirect']) && !$params['redirect'])
			return $tarefa;

		$this->redirect($this->uri->getOrigem());
	}

	public function viewAnotacao($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tarefa	= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		else
			$tarefa	= new \Entities\Intelihelp\Tarefa();

		$this->view->assign('tarefa', $tarefa);
		$this->view->assign('atribuir', isset($params['atribuir']));
		$this->view->assign('fechar', isset($params['fechar']));
	}

	public function saveAnotacao($params = array())
	{
		try
		{
			$idusuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();

			$tarefa		= $this->em->getRepository('\Entities\Intelihelp\Tarefa')->find($params['tarefa']['id_tarefa']);
			$status		= $this->em->getRepository('\Entities\Intelihelp\Status')->find($params['tarefa']['status']);
			$pessoa		= $this->em->getRepository('\Entities\Corp\PessoaFisica')->find($idusuario);

			$anotacao	= new \Entities\Intelihelp\Anotacao();
			$anotacao->setDsAnotacao($params['anotacao']['ds_anotacao']);
			$anotacao->setTarefa($tarefa);
			$anotacao->setResponsavel($pessoa);

			if (isset($_FILES['anotacao']['name']['id_arquivo']) && !empty($_FILES['anotacao']['name']['id_arquivo']))
			{
				$arquivo	= \Foxframe\FileUpload::upload($_FILES['anotacao'], 'id_arquivo');
				$anotacao->setArquivo($arquivo);
			}

			if ($tarefa->getStatus()->getIdStatus() != $params['tarefa']['status'])
			{
				$dsAnotacao	= "Status alterado: {$tarefa->getStatus()->getDsStatus()} => {$status->getDsStatus()}";
				if (!empty($params['anotacao']['ds_anotacao']))
				{
					$dsAnotacao .= "<hr>";
					$dsAnotacao .= $params['anotacao']['ds_anotacao'];
				}
				$anotacao->setDsAnotacao($dsAnotacao);
			}
			$anotacao->getTarefa()->setStatus($status);

			$this->em->persist($anotacao);
			$this->em->flush();

			$email = new \App\Intelihelp\Controller\mailController();
			$email->tarefaInteracao(array(
						'tarefa'	=> $tarefa,
						'anotacao'	=> $anotacao,
						'pessoa'	=> $pessoa
					)
				);

			$this->setFlash('Success', 'Sucesso', 'Tarefa salva com sucesso');
		}
		catch (Exception $e)
		{
			$this->setFlash('Error', 'Erro ao salvar tarefa', $e->getMessage());
		}
		$this->redirect(BASE_PATH.'intelihelp/tarefa/viewAnotacao/id/'.$tarefa->getIdTarefa());
	}

	public function viewAtribuir($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tarefa	= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		else
			$tarefa	= new \Entities\Intelihelp\Tarefa();

		$form	= new Form();
		$form->addValues($tarefa, 'tarefa');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tarefa'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveAtribuir'));
		$form->addElement(Input::create('tarefa[version]')->setType('hidden'));
		$form->addElement(Input::create('tarefa[id_tarefa]')->setType('hidden'));
		$form->addElement(
				Input::create('tarefa[dt_prazo]')
				->setType('text')
				->setLabel('Prazo')
				->addClass('datepicker')
				->validate()
		);
		$form->addElement(
				Select::create('tarefa[equipe]')
				->setLabel('Equipe')
				->validate()
				->setOption($this->em->getRepository('\Entities\Intelihelp\Equipe')->listComboExecutor())
				->setPlaceHolder('-- Selecione --')
		);
		$form->addElement(
				Select::create('tarefa[responsavel]')
				->setLabel('Responsável')
				->setOption($this->em->getRepository('\Entities\Intelihelp\Equipe')->listMembroCombo($tarefa->getEquipe()))
				->setPlaceHolder('-- Selecione --')
		);
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		$this->view->assign('tarefa', $tarefa);
		$this->returnAjax();
	}

	public function saveAtribuir($params = array())
	{
		try
		{
			$idusuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();
			$usuario	= $this->em->getRepository('\Entities\Corp\PessoaFisica')->find($idusuario);
			$tarefa		= $this->em->getRepository('\Entities\Intelihelp\Tarefa')->find($params['tarefa']['id_tarefa']);
			$equipe		= $this->em->getRepository('\Entities\Intelihelp\Equipe')->find($params['tarefa']['equipe']);
			$pessoa		= $this->em->getRepository('\Entities\Corp\Pessoa')->find($params['tarefa']['responsavel']);

			if ($tarefa->getEquipe() == $equipe
					&& $tarefa->getResponsavel() == $pessoa
					&& $tarefa->getDtPrazo()->format('d/m/Y') == \DateTime::createFromFormat('d/m/Y', $params['tarefa']['dt_prazo'])->format('d/m/Y'))
			{
				throw new \Exception('A atribuição da tarefa não foi alterada');
			}

			$dsanotacao = "Tarefa atribuída: ";
			if ($tarefa->getEquipe())
				$dsanotacao.= $tarefa->getEquipe()->getDsNome();
			if ($tarefa->getResponsavel())
				$dsanotacao.= " ({$tarefa->getResponsavel()->getDsNome()})";

			$dsanotacao.= " => {$equipe->getDsNome()}";
			if ($pessoa)
				$dsanotacao.= " ({$pessoa->getDsNome()})";

			$tarefa->setEquipe($equipe);
			$tarefa->setResponsavel($pessoa);
			$tarefa->setDtPrazo(\DateTime::createFromFormat('d/m/Y', $params['tarefa']['dt_prazo']));

			$anotacao	= new \Entities\Intelihelp\Anotacao();
			$anotacao->setDsAnotacao($dsanotacao);
			$anotacao->setTarefa($tarefa);
			$anotacao->setResponsavel($usuario);

			$this->em->persist($anotacao);
			$this->em->persist($tarefa);
			$this->em->flush();

			$email = new \App\Intelihelp\Controller\mailController();
			$email->tarefaAtribuida(array(
					'tarefa'	=> $tarefa,
				)
			);

			$this->setFlash('Success', 'Sucesso', 'Tarefa atribuída com sucesso');
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro ao atribuir tarefa', $e->getMessage());
		}
		$this->redirect($this->uri->getOrigem());
	}

	public function viewFechar($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tarefa	= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		else
			$tarefa	= new \Entities\Intelihelp\Tarefa();

		$form	= new Form();
		$form->addValues($tarefa, 'tarefa');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tarefa'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveFechar'));
		$form->addElement(Input::create('tarefa[version]')->setType('hidden'));
		$form->addElement(Input::create('tarefa[id_tarefa]')->setType('hidden'));
		$form->addElement(
				Select::create('tarefa[status]')
				->setLabel('Status')
				->setOption($this->em->getRepository('\Entities\Intelihelp\Status')->listCombo(array('s.fl_situacao' => 0)))
				->setPlaceHolder('-- Selecione --')
				->setValue(\Entities\Intelihelp\Status::FECHADO)
				->validate()
		);
		$form->addElement(
				TextArea::create('anotacao[ds_anotacao]')
				->setLabel('Observação')
				->addClass('simpleRich')
				->validate()
		);
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		$this->view->assign('tarefa', $tarefa);
		$this->returnAjax();
	}

	public function saveFechar($params = array())
	{
		try
		{
			$idusuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();

			$tarefa		= $this->em->getRepository('\Entities\Intelihelp\Tarefa')->find($params['tarefa']['id_tarefa']);
			$status		= $this->em->getRepository('\Entities\Intelihelp\Status')->find($params['tarefa']['status']);
			$usuario	= $this->em->getRepository('\Entities\Corp\Pessoa')->find($idusuario);

			$tarefa->setStatus($status);
			$tarefa->setDtFinalizacao(new \DateTime());
			if (!$tarefa->getResponsavel())
				$tarefa->setResponsavel($usuario);

			if ($params['anotacao']['ds_anotacao'] != '')
			{
				$anotacao	= new \Entities\Intelihelp\Anotacao();
				$anotacao->setDsAnotacao($params['anotacao']['ds_anotacao']);
				$anotacao->setTarefa($tarefa);
				$anotacao->setResponsavel($usuario);
				$this->em->persist($anotacao);
			}

			$this->em->persist($tarefa);
			$this->em->flush();

			$email = new \App\Intelihelp\Controller\mailController();
			$email->tarefaAvaliar(array(
					'tarefa'	=> $tarefa,
					'anotacao'	=> $anotacao,
					'pessoa'	=> $usuario
				)
			);

			$this->setFlash('Success', 'Sucesso', 'Tarefa fechada com sucesso');
		}
		catch (Exception $e)
		{
			$this->setFlash('Error', 'Erro ao fechar tarefa', $e->getMessage());
		}
		$this->redirect($this->uri->getOrigem());
	}

	public function viewAdicionarAnotacao($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tarefa	= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		else
			$tarefa	= new \Entities\Intelihelp\Tarefa();

		$form	= new Form();
		$form->addValues($tarefa, 'tarefa');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tarefa'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveAnotacao'));
		$form->addElement(Input::create('tarefa[version]')->setType('hidden'));
		$form->addElement(Input::create('tarefa[id_tarefa]')->setType('hidden'));
		$form->addElement(
				Select::create('tarefa[status]')
				->setLabel('Status')
				->validate()
				->setOption($this->em->getRepository('\Entities\Intelihelp\Status')->listCombo(array('s.fl_situacao' => 1)))
				->setPlaceHolder('-- Selecione --')
		);
		$form->addElement(TextArea::create('anotacao[ds_anotacao]')->setLabel('Anotação')->addClass('simpleRich'));
		$form->addElement(Input::create('anotacao[id_arquivo]')->setType('file')->setLabel('Arquivo'));
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		$this->view->assign('tarefa', $tarefa);
		return $this->returnAjax();
	}


	public function viewAvaliar($params = array())
	{
		$idusuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();
		$tarefa		= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		$avaliacao	= $this->em->getRepository('\Entities\Intelihelp\Avaliacao')->findOneBy(array('tarefa' => $params['id'], 'avaliador' => $idusuario));
		if (!$avaliacao)
			$avaliacao	= new \Entities\Intelihelp\Avaliacao();

		$form	= new Form();
		$form->addValues($tarefa, 'tarefa');
		$form->addValues($avaliacao, 'avaliacao');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tarefa'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveAvaliar'));
		$form->addElement(Input::create('tarefa[id_tarefa]')->setType('hidden'));
		$form->addElement(Input::create('avaliacao[version]')->setType('hidden'));
		$form->addElement(Input::create('avaliacao[id_avaliacao]')->setType('hidden'));
		$form->addElement(Radio::create('avaliacao[nt_tempo]')
				->setOption(array(1=>1,2=>2,3=>3,4=>4,5=>5))
				->addClass('star')
				->validate()
				->showLabel(false)
				->setLabel('Tempo'));
		$form->addElement(Radio::create('avaliacao[nt_qualidade]')
				->setOption(array(1=>1,2=>2,3=>3,4=>4,5=>5))
				->addClass('star')
				->validate()
				->showLabel(false)
				->setLabel('Qualidade'));
		$form->addElement(Radio::create('avaliacao[nt_cortesia]')
				->setOption(array(1=>1,2=>2,3=>3,4=>4,5=>5))
				->addClass('star')
				->validate()
				->showLabel(false)
				->setLabel('Cortesia'));
		$form->addElement(Radio::create('avaliacao[nt_expectativa]')
				->setOption(array(1=>1,2=>2,3=>3,4=>4,5=>5))
				->addClass('star')
				->validate()
				->showLabel(false)
				->setLabel('Expectativa'));
		$form->addElement(TextArea::create('avaliacao[ds_melhoria]')
				->setLabel('Melhorias:')
				->addClass('simpleRich'));
		$form->addElement(TextArea::create('avaliacao[ds_observacao]')
				->setLabel('Observações:')
				->addClass('simpleRich'));
		$this->view->assign('form', $form);
		$this->view->assign('tarefa', $tarefa);
		$this->view->assign('avaliacao', $avaliacao);
	}

	public function saveAvaliar($params = array())
	{
		try
		{
			$idusuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();
			$usuario	= $this->em->getRepository('\Entities\Corp\PessoaFisica')->find($idusuario);
			$tarefa		= $this->em->getRepository('\Entities\Intelihelp\Tarefa')->find($params['tarefa']['id_tarefa']);

			if (isset($params['avaliacao']['id_avaliacao']) && !empty($params['avaliacao']['id_avaliacao']))
				$avaliacao	= $this->em->find('\Entities\Intelihelp\Avaliacao', $params['avaliacao']['id_avaliacao']);
			else
				$avaliacao	= new \Entities\Intelihelp\Avaliacao();

			$avaliacao->setTarefa($tarefa);
			$avaliacao->setAvaliador($usuario);
			$avaliacao->setVersion($params['avaliacao']['version']);
			if (isset($params['avaliacao']['nt_cortesia']))
				$avaliacao->setNtCortesia($params['avaliacao']['nt_cortesia']);
			if (isset($params['avaliacao']['nt_expectativa']))
				$avaliacao->setNtExpectativa($params['avaliacao']['nt_expectativa']);
			if (isset($params['avaliacao']['nt_qualidade']))
				$avaliacao->setNtQualidade($params['avaliacao']['nt_qualidade']);
			if (isset($params['avaliacao']['nt_tempo']))
				$avaliacao->setNtTempo($params['avaliacao']['nt_tempo']);
			$avaliacao->setDsMelhoria($params['avaliacao']['ds_melhoria']);
			$avaliacao->setDsObservacao($params['avaliacao']['ds_observacao']);

			$this->em->persist($avaliacao);
			$this->em->flush();

			$this->setFlash('Success', 'Sucesso', 'Registro de avaliação salvo com sucesso');
		}
		catch (Exception $e)
		{
			$this->setFlash('Error', 'Erro ao registrar avaliação tarefa', $e->getMessage());
		}
		$this->redirect(BASE_PATH.'intelihelp/tarefa/viewListarTarefasFechadas');
	}

	public function viewReabrir($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tarefa	= $this->em->find('\Entities\Intelihelp\Tarefa', $params['id']);
		else
			$tarefa	= new \Entities\Intelihelp\Tarefa();

		$form	= new Form();
		$form->addValues($tarefa, 'tarefa');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tarefa'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveReabrir'));
		$form->addElement(Input::create('tarefa[version]')->setType('hidden'));
		$form->addElement(Input::create('tarefa[id_tarefa]')->setType('hidden'));
		$form->addElement(
				Input::create('tarefa[dt_prazo]')
				->setType('text')
				->setLabel('Prazo')
				->addClass('datepicker')
				->validate()
		);
		$form->addElement(TextArea::create('anotacao[ds_anotacao]')->setLabel('Motivo')->addClass('simpleRich')->validate());
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		$this->view->assign('tarefa', $tarefa);
		$this->returnAjax();
	}

	public function saveReabrir($params = array())
	{
		try
		{
			$idusuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario'])->getIdPessoa();
			$usuario	= $this->em->getRepository('\Entities\Corp\PessoaFisica')->find($idusuario);
			$tarefa		= $this->em->getRepository('\Entities\Intelihelp\Tarefa')->find($params['tarefa']['id_tarefa']);
			$status		= $this->em->getRepository('\Entities\Intelihelp\Status')->find(\Entities\Intelihelp\Status::ABERTO);

			$tarefa->setDtPrazo(\DateTime::createFromFormat('d/m/Y', $params['tarefa']['dt_prazo']));
			$tarefa->setDtFinalizacao();
			$tarefa->setStatus($status);

			$dsanotacao = "Tarefa reaberta pelo(s) motivo(s) abaixo:<hr>";
			$dsanotacao.= $params['anotacao']['ds_anotacao'];
			$anotacao	= new \Entities\Intelihelp\Anotacao();
			$anotacao->setDsAnotacao($dsanotacao);
			$anotacao->setTarefa($tarefa);
			$anotacao->setResponsavel($usuario);

			$this->em->persist($anotacao);
			$this->em->persist($tarefa);
			$this->em->flush();

			$email = new \App\Intelihelp\Controller\mailController();
			$email->tarefaReaberta(array(
					'tarefa'	=> $tarefa,
					'anotacao'	=> $anotacao
				)
			);

			$this->setFlash('Success', 'Sucesso', 'Tarefa reaberta com sucesso');
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro ao reabrir tarefa', $e->getMessage());
		}
		$this->redirect($this->uri->getOrigem());
	}
}